package controller;

import util.HttpUtil;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class Download {
    //客户端下载
    public static String downFile(String uuid, String downPath) throws Exception {
        HttpURLConnection connection = HttpUtil.creatUtil("http://127.0.0.1:8080/test/download", "GET");
        connection.setRequestProperty("uuid", uuid);
        connection.connect();
        //判断在获取过程中状态码是否出现异常
        if (connection.getResponseCode() != 200) {
            return "出现异常，状态码为：" + connection.getResponseCode();
        } else {
            InputStream in = connection.getInputStream();
            String fileType = connection.getHeaderField("filetype");
            int length = in.available();
            //使用输出流下载文件
            OutputStream out = new FileOutputStream("D:\\java\\qys\\" + uuid + fileType);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buf)) > 0) {
                out.write(buf, 0, bytesRead);
            }
            return "下载完毕";
        }
    }

}
